package com.eclipse.e20200131_模拟连接池.com.qf.app1.m4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Test1 {
	private static Logger logger = LogManager.getLogger();

	private static Properties config = new Properties();

	private static String diverclass = null;

	private static String url = null;

	private static String userName = null;

	private static String password = null;

	// 记载配置文件，读取jdbc的配置信息
	static {
		try {
			config.load(JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties"));
			url = config.getProperty("jdbc.url");
			userName = config.getProperty("jdbc.username");
			password = config.getProperty("jdbc.password");
			diverclass = config.getProperty("jdbc.diverclass");
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex);
		}
	}

	public static void main(String[] args) throws Exception {
		//测试连接所花费的毫秒数
		long start = System.currentTimeMillis();
		for (int i = 0; i < 100; i++) {
			Connection connection1 = getConnection();
			connection1.close();
		}
	
		logger.debug(System.currentTimeMillis() - start);

	}

	/**
	 * 打开连接
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() {
		try {
			// 1:加载驱动类
			Class.forName(diverclass);

			// 2: 打开连接
			Connection connection = DriverManager.getConnection(url, userName, password);
			return connection;
		} catch (ClassNotFoundException ex) {
			throw new RuntimeException("数据库驱动没导入", ex);
		} catch (SQLException ex) {
			throw new RuntimeException("url或者用户名或者密码是错误", ex);
		}
	}

}
